unit UBoleto;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, ExtCtrls, DB, DBTables, StdCtrls, Mask, DBCtrls, ComCtrls,
  Grids, DBGrids, Buttons, ToolWin, QRCtrls, QuickRpt, ActnList;

type
  Tfboleto = class(TForm)
    PageControl1: TPageControl;
    TabSheet1: TTabSheet;
    TabSheet2: TTabSheet;
    Panel2: TPanel;
    ScrollBox: TScrollBox;
    dsboleto: TDataSource;
    tbboleto: TTable;
    Label1: TLabel;
    DBMemo1: TDBMemo;
    tbcomplemento: TTable;
    dscomplemento: TDataSource;
    Label18: TLabel;
    ToolBar2: TToolBar;
    btnPrimeiro: TSpeedButton;
    btnVoltar: TSpeedButton;
    btnAvancar: TSpeedButton;
    btnUltimo: TSpeedButton;
    Panel6: TPanel;
    Panel7: TPanel;
    btnnovo: TSpeedButton;
    Panel5: TPanel;
    btngravar: TSpeedButton;
    Panel8: TPanel;
    btncancelar: TSpeedButton;
    Panel12: TPanel;
    SpeedButton21: TSpeedButton;
    Panel11: TPanel;
    Panel3: TPanel;
    btnimprimir: TSpeedButton;
    Panel9: TPanel;
    Panel14: TPanel;
    Panel1: TPanel;
    Label2: TLabel;
    Label3: TLabel;
    EditNlocal: TDBEdit;
    EditLocal: TDBEdit;
    Label4: TLabel;
    EditCedente: TDBEdit;
    EditNcedente: TDBEdit;
    Label5: TLabel;
    Label8: TLabel;
    EditEspecie: TDBEdit;
    EditNdocumento: TDBEdit;
    Label7: TLabel;
    EditDatadocumento: TDBEdit;
    Label6: TLabel;
    Label9: TLabel;
    EditAceite: TDBEdit;
    EditDataprocesso: TDBEdit;
    Label11: TLabel;
    EditUsobanco: TDBEdit;
    Bevel1: TBevel;
    EditCarteira: TDBEdit;
    Label12: TLabel;
    Label15: TLabel;
    EditCgccpf: TDBEdit;
    Label17: TLabel;
    EditCodigobaixa: TDBEdit;
    EditSacadoAvalista: TDBEdit;
    Label16: TLabel;
    EditSacado: TDBEdit;
    Label14: TLabel;
    Bevel2: TBevel;
    Label19: TLabel;
    EditVencimento: TDBEdit;
    Label23: TLabel;
    EditDesconto: TDBEdit;
    EditMulta: TDBEdit;
    Label24: TLabel;
    EditAgCodigoCedente: TDBEdit;
    Label20: TLabel;
    EditNossonumero: TDBEdit;
    Label21: TLabel;
    Label25: TLabel;
    EditTotal: TDBEdit;
    Label22: TLabel;
    EditValor: TDBEdit;
    Bevel3: TBevel;
    Label10: TLabel;
    Panel4: TPanel;
    Panel10: TPanel;
    Label26: TLabel;
    Edit1: TEdit;
    DBGrid1: TDBGrid;
    ActionList1: TActionList;
    formatar: TAction;
    ajuda: TAction;
    foco: TAction;
    QuickRep1: TQuickRep;
    QRBand1: TQRBand;
    QRDBText13: TQRDBText;
    QRDBText14: TQRDBText;
    LINSTRUCAO: TQRLabel;
    QRDBText12: TQRDBText;
    QRDBText17: TQRDBText;
    QRDBText18: TQRDBText;
    QRDBText19: TQRDBText;
    QRDBText20: TQRDBText;
    QRDBText22: TQRDBText;
    procedure DBMemo1Exit(Sender: TObject);
    procedure btngravarClick(Sender: TObject);
    procedure btncancelarClick(Sender: TObject);
    procedure SpeedButton21Click(Sender: TObject);
    procedure tbboletoAfterOpen(DataSet: TDataSet);
    procedure tbboletoAfterScroll(DataSet: TDataSet);
    procedure tbboletoBeforeEdit(DataSet: TDataSet);
    procedure FormKeyPress(Sender: TObject; var Key: Char);
    procedure FormActivate(Sender: TObject);
    procedure Edit1Change(Sender: TObject);
    procedure btnPrimeiroClick(Sender: TObject);
    procedure btnnovoClick(Sender: TObject);
    procedure btnVoltarClick(Sender: TObject);
    procedure btnAvancarClick(Sender: TObject);
    procedure btnUltimoClick(Sender: TObject);
    procedure btnimprimirClick(Sender: TObject);
    procedure PageControl1Change(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure formatarExecute(Sender: TObject);
    procedure EditValorExit(Sender: TObject);
    procedure ajudaExecute(Sender: TObject);
    procedure focoExecute(Sender: TObject);
    procedure Label2Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  fboleto: Tfboleto;

implementation

{$R *.dfm}

procedure Tfboleto.DBMemo1Exit(Sender: TObject);
begin
        tbcomplemento.Edit;
         tbcomplemento.post;
end;

procedure Tfboleto.btngravarClick(Sender: TObject);
begin
         tbboleto.Edit;
          tbboleto.Post;
           btngravar.Enabled:=false;
            btncancelar.Enabled:=false;
end;

procedure Tfboleto.btncancelarClick(Sender: TObject);
begin
        tbboleto.cancel;
          btnGravar.Enabled:=false;
           btncancelar.enabled := false;
end;

procedure Tfboleto.SpeedButton21Click(Sender: TObject);
begin
          if Application.MessageBox('Deseja excluir este cliente? ',
         'Confirme', 4)= idYes then begin

        tbboleto.delete;
          btnGravar.Enabled:=false;
           btncancelar.enabled := false;
        end;

end;

procedure Tfboleto.tbboletoAfterOpen(DataSet: TDataSet);
begin
        btngravar.enabled := false;
        btncancelar.enabled := false;
end;

procedure Tfboleto.tbboletoAfterScroll(DataSet: TDataSet);
begin
           btnAvancar.Enabled := True;
            btnUltimo.Enabled := True;
             btnPrimeiro.Enabled := True;
              btnVoltar.Enabled := True;

        if tbboleto.Bof then begin
         btnPrimeiro.Enabled := False;
          btnVoltar.Enabled := False;

             end;

        if tbboleto.Eof then begin

           btnAvancar.Enabled := False;
            btnUltimo.Enabled := False;
             end;

end;

procedure Tfboleto.tbboletoBeforeEdit(DataSet: TDataSet);
begin
       btnGravar.Enabled:=True;
       btncancelar.enabled := true;

end;

procedure Tfboleto.FormKeyPress(Sender: TObject; var Key: Char);
begin
 if key=#13 then begin
  key := #0;
       Perform(WM_nextdlgctl,0,0);
 end Else if Key = #27 then
   Perform(WM_nextdlgctl,1,0);
end;

procedure Tfboleto.FormActivate(Sender: TObject);
begin

        tbboleto.Open;
        tbcomplemento.open;
        pagecontrol1.ActivePageIndex := 0;
        formatarExecute(self);        
end;

procedure Tfboleto.Edit1Change(Sender: TObject);
begin
        tbboleto.FindNearest([edit1.text]);
end;

procedure Tfboleto.btnPrimeiroClick(Sender: TObject);
begin
        tbboleto.First;
end;

procedure Tfboleto.btnnovoClick(Sender: TObject);
begin
        tbboleto.Insert;
         editnlocal.SetFocus;
end;

procedure Tfboleto.btnVoltarClick(Sender: TObject);
begin
        tbboleto.Prior;
end;

procedure Tfboleto.btnAvancarClick(Sender: TObject);
begin
        tbboleto.Next;
end;

procedure Tfboleto.btnUltimoClick(Sender: TObject);
begin
        tbboleto.Last;
end;

procedure Tfboleto.btnimprimirClick(Sender: TObject);
var
bookmark : tbookmarkstr;
begin

     bookmark := tbboleto.Bookmark;

        linstrucao.Caption := dbmemo1.Text;

        tbboleto.Filter := 'sacado= ''' + editsacado.Text + '''';
        tbboleto.Filtered := true;

        quickrep1.PreviewModal;
        tbboleto.Filtered := false;

        tbboleto.Bookmark := bookmark;

end;

procedure Tfboleto.PageControl1Change(Sender: TObject);
begin
        IF pagecontrol1.ActivePageIndex = 0 then
         keypreview := true
          else
         keypreview := false; 

end;

procedure Tfboleto.FormClose(Sender: TObject; var Action: TCloseAction);
begin
        tbboleto.close;
        tbcomplemento.close;
end;

procedure Tfboleto.formatarExecute(Sender: TObject);
begin

 (tbboleto.fieldbyname ('nlocal') as tfloatfield).displayformat := '000';
 (tbboleto.fieldbyname ('datadocumento') as tdatetimefield).editmask := '99/99/9999';
 (tbboleto.fieldbyname ('dataprocesso') as tdatetimefield).editmask := '99/99/9999';
 (tbboleto.fieldbyname ('vencimento') as tdatetimefield).editmask := '99/99/9999';
 (tbboleto.fieldbyname ('valor') as tfloatfield).displayformat := '###,##0.00';
 (tbboleto.fieldbyname ('desconto') as tfloatfield).displayformat := '###,##0.00';
 (tbboleto.fieldbyname ('multa') as tfloatfield).displayformat := '###,##0.00';
 (tbboleto.fieldbyname ('total') as tfloatfield).displayformat := '###,##0.00';

end;

procedure Tfboleto.EditValorExit(Sender: TObject);

begin
       tbboleto.edit;
        tbboleto[ 'total'] := tbboleto['valor'] - tbboleto['desconto'] + tbboleto['multa'];
end;

procedure Tfboleto.ajudaExecute(Sender: TObject);
var
  Comp: Trichedit; // * variável Comp da classe TMemo
  texto : string;
begin
  Comp := Trichedit.Create (Self);  // * cria o componente Memo
  Comp.Parent := Self;    // * exibe o componente criado

  if fileexists (extractfilepath(paramstr(0)) + '\ajuda\ajudaboleto.txt') then
   comp.lines.loadfromfile (extractfilepath(paramstr(0)) + '\ajuda\ajudaboleto.txt');

  texto := comp.Text;
  comp.Destroy;

  Showmessage (texto);


end;

procedure Tfboleto.focoExecute(Sender: TObject);
begin
        if edit1.Focused = true then
         editnlocal.SetFocus
        else
        edit1.SetFocus; 
end;

procedure Tfboleto.Label2Click(Sender: TObject);
var
st:string;
begin
        if inputquery('Localizar boleto','Entre com o número:',ST) then
          if not tbboleto.Locate ('Nlocal',ST,[]) then
           ShowMessage ('Boleto não localizado !!!');

end;

end.
